<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- unicode.qdoc -->
  <title>Unicode in Qt | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Unicode in Qt</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#qt-s-classes-for-working-with-strings">Qt's Classes for Working with Strings</a></li>
<li class="level1"><a href="#information-about-unicode-on-the-web">Information about Unicode on the Web</a></li>
<li class="level1"><a href="#unicode-in-qt">Unicode in Qt</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Unicode in Qt</h1>
<span class="subtitle"></span>
<!-- $$$unicode.html-description -->
<div class="descr"> <a name="details"></a>
<p><a href="http://www.unicode.org/">Unicode</a> is the standard for encoding text in almost all languages spoken in the world. It is nowadays used as the native encoding for text on most modern operating systems. The major exception is Microsoft Windows that still has a dual system supporting code pages and Unicode for applications.</p>
<a name="qt-s-classes-for-working-with-strings"></a>
<h2 id="qt-s-classes-for-working-with-strings">Qt's Classes for Working with Strings</h2>
<p>These classes are relevant when working with string data. For information about rendering text, see the <a href="../qtgui/richtext.html">Rich Text Processing</a> overview, and if your string data is in XML, see the <a href="../qtxml/xml-processing.html">XML Processing</a> overview.</p>
<div class="table"><table class="annotated">
<tr class="odd topAlign"><td class="tblName"><p><a href="../qtcore/qbytearray.html">QByteArray</a></p></td><td class="tblDescr"><p>Array of bytes</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="../qtcore/qbytearraylist.html">QByteArrayList</a></p></td><td class="tblDescr"><p>List of byte arrays</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="../qtcore/qbytearraymatcher.html">QByteArrayMatcher</a></p></td><td class="tblDescr"><p>Holds a sequence of bytes that can be quickly matched in a byte array</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="../qtcore/qchar.html">QChar</a></p></td><td class="tblDescr"><p>16-bit Unicode character</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="../qtcore/qcollator.html">QCollator</a></p></td><td class="tblDescr"><p>Compares strings according to a localized collation algorithm</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="../qtcore/qcollatorsortkey.html">QCollatorSortKey</a></p></td><td class="tblDescr"><p>Can be used to speed up string collation</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="../qtcore/qlatin1char.html">QLatin1Char</a></p></td><td class="tblDescr"><p>8-bit ASCII/Latin-1 character</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="../qtcore/qlatin1string.html">QLatin1String</a></p></td><td class="tblDescr"><p>Thin wrapper around an US-ASCII/Latin-1 encoded string literal</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="../qtcore/qlocale.html">QLocale</a></p></td><td class="tblDescr"><p>Converts between numbers and their string representations in various languages</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="../qtcore/qstaticbytearraymatcher.html">QStaticByteArrayMatcher</a></p></td><td class="tblDescr"><p>Compile-time version of QByteArrayMatcher</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="../qtcore/qstring.html">QString</a></p></td><td class="tblDescr"><p>Unicode character string</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="../qtcore/qstringlist.html">QStringList</a></p></td><td class="tblDescr"><p>List of strings</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="../qtcore/qstringmatcher.html">QStringMatcher</a></p></td><td class="tblDescr"><p>Holds a sequence of characters that can be quickly matched in a Unicode string</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="../qtcore/qstringref.html">QStringRef</a></p></td><td class="tblDescr"><p>Thin wrapper around QString substrings</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="../qtcore/qstringview.html">QStringView</a></p></td><td class="tblDescr"><p>Unified view on UTF-16 strings with a read-only subset of the QString API</p></td></tr>
<tr class="even topAlign"><td class="tblName"><p><a href="../qtcore/qtextboundaryfinder.html">QTextBoundaryFinder</a></p></td><td class="tblDescr"><p>Way of finding Unicode text boundaries in a string</p></td></tr>
<tr class="odd topAlign"><td class="tblName"><p><a href="../qtcore/qtextstream.html">QTextStream</a></p></td><td class="tblDescr"><p>Convenient interface for reading and writing text</p></td></tr>
</table></div>
<a name="information-about-unicode-on-the-web"></a>
<h2 id="information-about-unicode-on-the-web">Information about Unicode on the Web</h2>
<p>The <a href="http://www.unicode.org/">Unicode Consortium</a> has a number of documents available, including</p>
<ul>
<li><a href="http://www.unicode.org/versions/latest/">The current version of the standard</a></li>
<li><a href="http://www.unicode.org/unicode/standard/principles.html">A technical introduction to Unicode</a></li>
<li><a href="http://www.unicode.org/unicode/standard/standard.html">The home page for the standard</a></li>
</ul>
<a name="unicode-in-qt"></a>
<h2 id="unicode-in-qt">Unicode in Qt</h2>
<p>In Qt, and in most applications that use Qt, most or all user-visible strings are stored using Unicode. Qt provides:</p>
<ul>
<li>Translation to/from legacy encodings for file I/O: see <a href="../qtcore/qtextcodec.html">QTextCodec</a> and <a href="../qtcore/qtextstream.html">QTextStream</a>.</li>
<li>Support for locale specific Input Methods and keyboards.</li>
<li>A string class, <a href="../qtcore/qstring.html">QString</a>, that stores Unicode characters, with support for migrating from C strings including fast translation to and from UTF-8, ISO8859-1 and US-ASCII, and all the usual string operations.</li>
<li>Unicode-aware UI controls.</li>
<li>Unicode compliant text segmentation (<a href="../qtcore/qtextboundaryfinder.html">QTextBoundaryFinder</a>)</li>
<li>Unicode compliant line breaking and text rendering</li>
</ul>
<p>To fully benefit from Unicode, we recommend using <a href="../qtcore/qstring.html">QString</a> for storing all user-visible strings, and performing all text file I/O using <a href="../qtcore/qtextstream.html">QTextStream</a>.</p>
<p>All the function arguments in Qt that may be user-visible strings, <a href="../qtwidgets/qlabel.html#text-prop">QLabel::setText</a>() and a many others, take <code>const QString &amp;</code>s. <a href="../qtcore/qstring.html">QString</a> provides implicit casting from <code>const char *</code> so that things like</p>
<pre class="cpp">

  label<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="string">&quot;Password:&quot;</span>);

</pre>
<p>will work. There is also a function, <a href="../qtcore/qobject.html#tr">QObject::tr</a>(), that provides translation support, like this:</p>
<pre class="cpp">

  label<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Password:&quot;</span>));

</pre>
<p><a href="../qtcore/qobject.html#tr">QObject::tr</a>() maps from <code>const char *</code> to a Unicode string, and uses installable <a href="../qtcore/qtranslator.html">QTranslator</a> objects to do the mapping.</p>
<p>Qt provides a number of built-in <a href="../qtcore/qtextcodec.html">QTextCodec</a> classes, that is, classes that know how to translate between Unicode and legacy encodings to support programs that must talk to other programs or read/write files in legacy file formats.</p>
<p>Conversion to/from <code>const char *</code> uses a UTF-8. However, applications can easily find codecs for other locales, and set any open file or network connection to use a special codec.</p>
<p>Since US-ASCII and ISO-8859-1 are so common, there are also especially fast functions for mapping to and from them. For example, to open an application's icon one might do this:</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qfile.html">QFile</a></span> file(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>fromLatin1(<span class="string">&quot;appicon.png&quot;</span>));

</pre>
<p>or</p>
<pre class="cpp">

  <span class="type"><a href="../qtcore/qfile.html">QFile</a></span> file(QLatin1String(<span class="string">&quot;appicon.png&quot;</span>));

</pre>
<p>Qt supports rendering text in most languages written in the world. The detailed list of supported writing systems depends a bit on operating system support and font availability on the target system.</p>
</div>
<p><b>See also </b><a href="internationalization.html">Internationalization with Qt</a>.</p>
<!-- @@@unicode.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
